import Vue from 'vue';
import Vuex from 'vuex';
import config from '@/common/config';

Vue.use(Vuex);

const setKey = (key) => {
  return `${config.ENV}_${key}`;
};

const store = new Vuex.Store({
  state: {
    token: '',
    userInfo: {},
  },
  mutations: {
    refreshToken(state, token) {
      state.token = token;
      uni.setStorageSync(setKey('token'), token);
    },
    refreshUserInfo(state, userInfo) {
      state.userInfo = userInfo;
    },
  },
  getters: {
    token(state) {
      const token = uni.getStorageSync(setKey('token')) || '';
      state.token = token;
      return state.token;
    },
    getUserInfo(state) {
      return state.userInfo;
    },
  },
});

export default store;
